<template>
  <div class="wrapper">
    <el-card class="form-wrapper" shadow="never">
      <el-form class="form" :model="formData" inline>
        <el-form-item label="商家名称">
          <el-input v-model="formData.username" clearable @keyup.enter="resetPage" />
        </el-form-item>
        <el-form-item label="业务员" prop="merchantId">
          <el-select v-model="formData.salesmanId" filterable remote reserve-keyword placeholder="输入搜索"
            :remote-method="searchSalesmans" :loading="loading" style="width: 240px">
            <el-option v-for="item in salesmans" :key="item.id" :label="item.nickname" :value="item.id" />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
      <el-button type="primary" size="small" plain @click="handleAdd">
        <template #icon>
          <icon name="el-icon-Plus" />
        </template>
        新增
      </el-button>
    </el-card>
    <el-card v-loading="pager.loading" shadow="never">
      <el-table :data="pager.lists" size="large">
        <el-table-column prop="name" width="50" type="expand">
          <template #default="props">
            <el-descriptions class="margin-top" :title="props.row.name" :column="3" border>
              <el-descriptions-item>
                <template #label>
                  <div class="cell-item">
                    <el-icon>
                      <user />
                    </el-icon>
                    商家名称
                  </div>
                </template>
                {{ props.row.name }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template #label>
                  <div class="cell-item">
                    <el-icon>
                      <peple />
                    </el-icon>
                    联系人
                  </div>
                </template>
                {{ props.row.contactName }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template #label>
                  <div class="cell-item">
                    <el-icon>
                      <iphone />
                    </el-icon>
                    联系人电话:
                  </div>
                </template>
                {{ props.row.contactPhone }}
              </el-descriptions-item>

              <el-descriptions-item>
                <template #label>
                  <div class="cell-item">
                    <el-icon>
                      <office-building />
                    </el-icon>
                    商家地址
                  </div>
                </template>
                {{ props.row.address }}
              </el-descriptions-item>
            </el-descriptions>
          </template>
        </el-table-column>
        <el-table-column label="序号" prop="id" width="60" />
        <el-table-column label="商家" prop="name" width="250">
        </el-table-column>
        <el-table-column label="活动数量" prop="activityCount" />
        <!-- <el-table-column label="地址" prop="address" width="200" show-overflow-tooltip />
        <el-table-column label="联系人" prop="contactName" width="100" />
        <el-table-column label="电话" prop="contactPhone" width="200">
        </el-table-column> -->
        <el-table-column label="业务员" prop="salesman" />
        <el-table-column label="创建时间" prop="createdTime" />
        <el-table-column label="修改时间" prop="updatedTime" />
        <el-table-column label="状态" min-width="100" fixed="right">
          <template #default="{ row }">
            <el-tag v-if="row.status == 0" type="info">停用</el-tag>
            <el-tag v-else-if="row.status == 1" type="success">正常</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="100px">
          <template #default="scope">
            <el-dropdown>
              <el-button link type="primary" plain>操作</el-button>
              <template #dropdown>
                <el-dropdown-menu>
                  <!-- <el-dropdown-item @click="">分配</el-dropdown-item> -->
                  <el-dropdown-item @click="handleEdit(scope.row)">编辑</el-dropdown-item>
                  <el-dropdown-item @click="handleDelete(scope.row.id)">删除</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>
    <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
  </div>
</template>

<script lang="ts" setup>
import {
  listsApi,
  deleteApi
} from '@/api/merchant'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import { nextTick, onMounted, reactive, ref, shallowRef } from 'vue'
import pagination from '@/components/pagination/index.vue'
import { listsApi as getSalesmanListsApi } from '@/api/salesman'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 表单数据
const formData = reactive({
  salesmanId: '',
  username: '',
  nickname: '',
  role: '',
})
const showEdit = ref(false)
const { pager, getLists, resetParams, resetPage } = usePaging({
  fetchFun: listsApi,
  params: formData,
})
const loading = ref(false)
const salesmans = ref<any>([])
const searchSalesmans = (name: string) => {
  loading.value = true
  salesmans.value = []
  getSalesmanListsApi({
    name,
    pageSize: 10,
  })
    .then(data => {
      salesmans.value = data.lists
      loading.value = false
    })
    .catch(() => {
      loading.value = false
      salesmans.value = []
    })
}
const changeStatus = async (active: any, id: number) => {
  // try {
  //   await feedback.confirm(`确定${active ? '停用' : '开启'}当前管理员？`)
  //   await changeUserStatusApi({ id })
  //   feedback.msgSuccess('修改成功')
  //   getLists()
  // } catch (error) {
  //   getLists()
  // }
}
const handleAdd = async () => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('edit')
  editRef.value?.setFormData(data)
}

const handleDelete = async (id: number) => {
  await feedback.confirm('确定要删除？')
  await deleteApi({ id })
  feedback.msgSuccess('删除成功')
  getLists()
}

onMounted(() => {
  getLists()
})
</script>
<style lang="scss" scoped>
.text_hidden {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
  overflow: hidden;
}

.wrapper {
  .form-wrapper {
    padding-bottom: 0px;

    .form {
      // margin-bottom: 14px;
    }
  }
}

.pagination {
  // class="flex mt-4 justify-end"
  display: flex;
  justify-content: end;
}

.el-descriptions {
  margin-top: 20px;
}

.cell-item {
  display: flex;
  align-items: center;
}

.margin-top {
  margin-top: 20px;
}
</style>
